package com.wy.service.impl;

import com.wy.dao.CodeDao;
import com.wy.pojo.Code;
import com.wy.service.CodeService;
import com.wy.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Random;

/**
 * description CodeServiceImpl
 * author wy
 * date 2021-09-02 21:48
 **/
@Slf4j
@Service
public class CodeServiceImpl implements CodeService
{

    @Autowired
    private CodeDao codeDao;

    @Override
    @Transactional
    public void addCode(String email, String codestr)
    {
        Code code = new Code();
        code.setEmail(email);
        code.setCode(codestr);
        code.setCreatetime(new Date());
        code.setExpiretime(new Date(System.currentTimeMillis() + 100000));
        codeDao.saveAndFlush(code);
    }

    @Override
    public Code getCodeByEmail(String email)
    {
        Code code = new Code();
        code.setEmail(email);
        Example<Code> example = Example.of(code);
        if (codeDao.findOne(example).isPresent()) {
            return codeDao.findOne(example).get();
        }
        return null;
    }
}
